package com.cyt.work.question.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.question.dao.QuestionSubjectDao;
import com.cyt.work.question.model.po.QuestionSubject;
import com.cyt.work.question.model.po.QuestionSubjectSelect;
import com.cyt.work.question.model.vo.QuestionSubjectVo;
import com.cyt.work.question.service.IQuestionSubjectSelectService;
import com.cyt.work.question.service.IQuestionSubjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 问卷题目表 service实现
 *
 * @author concong tao
 * @since 2019-01-31
 */
@Service
public class QuestionSubjectServiceImpl extends BaseServiceImpl<QuestionSubject> implements IQuestionSubjectService {

    @Autowired
    private IQuestionSubjectSelectService questionSubjectSelectService;
    @Autowired
    private QuestionSubjectDao questionSubjectDao;
    @Autowired
    public void setBaseDao(QuestionSubjectDao questionSubjectDao) {
        this.baseDao = questionSubjectDao;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ReturnResult saveQuestionSubject(String questionId, String status, String applyId, String subjectJson) {
        // 转为subject
        List<QuestionSubject> subjectList = JSONArray.parseArray(subjectJson, QuestionSubject.class);
        // 分别保存
        if (subjectList == null || subjectList.size() == 0) {
            if (StringUtils.equals(status, BaseConstant.QUESTION_STATUS_EDIT)) {
                return new ReturnResult();
            } else {
                return new ReturnResult(ErrorCode.CODE_1, "请添加题目，否则不能发布");
            }
        }
        List<QuestionSubjectSelect> subjectSelectList = new ArrayList<>();
        for (QuestionSubject subject : subjectList) {
            // 保存，并获取到主键，然后分别保存
            subject.setId(BaseUtil.uId());
            subject.setQuestionId(questionId);
            subject.setApplyId(applyId);
            subject.setCreateTime(new Date());
            int subjectFlag = this.insert(subject);
            if (subjectFlag <= 0) {
                continue;
            }
            List<QuestionSubjectSelect> selectList = subject.getSelectList();
            if (selectList == null || selectList.size() == 0) {
                continue;
            }
            String subjectId = subject.getId();
            // 获取到A,B
            char numStr = 'A';
            for (QuestionSubjectSelect subjectSelect : selectList) {
                subjectSelect.setId(BaseUtil.uId());
                subjectSelect.setApplyId(applyId);
                subjectSelect.setNum(String.valueOf(numStr));
                subjectSelect.setQuestionId(questionId);
                subjectSelect.setSubjectId(subjectId);
                subjectSelectList.add(subjectSelect);
                numStr++;
            }
        }
        int info = questionSubjectSelectService.insertList(subjectSelectList);
        if (info <= 0) {
            return new ReturnResult(ErrorCode.CODE_1,ErrorCode.CODE_ADD_FAIL_MSG);
        }
        return new ReturnResult();
    }

    @Override
    public List<QuestionSubject> acceptList(QuestionSubjectVo questionSubjectVo) {
        Example example = new Example(QuestionSubject.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(questionSubjectVo.getQuestionId())) {
            criteria.andEqualTo("questionId", questionSubjectVo.getQuestionId());
        }
        example.orderBy("orderNum");
        return questionSubjectDao.selectByExample(example);
    }

    @Override
    public void insertList(List<QuestionSubject> list) {
        questionSubjectDao.insertList(list);
    }
}
